<?php

namespace app\models;

use app\components\BaseActiveRecord;

/**
 * This is the model class for table "{{%user_platform}}".
 *
 * @property integer $id
 * @property integer $uid
 * @property string $open_id
 * @property string $union_id
 * @property string $user_info
 * @property integer $type
 * @property integer $ctime
 * @property User $user
 */
class UserPlatform extends BaseActiveRecord {

    const TYPE_WEIBO = 1;
    const TYPE_WEIXIN = 2;
    const TYPE_QQ = 3;

    public static $typeLabels = [
        self::TYPE_WEIBO => '微博',
        self::TYPE_WEIXIN => '微信',
        self::TYPE_QQ => 'QQ',
    ];

    public static $typeNames = [
        'weixin' => self::TYPE_WEIXIN,
        'qq' => self::TYPE_QQ,
        'weibo' => self::TYPE_WEIBO
    ];

    public function init() {
        parent::init();
        $this->user_info = '';
    }

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return '{{%user_platform}}';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['open_id', 'type'], 'required'],
            ['type', 'in', 'range' => self::getTypes()],
            [['uid', 'type', 'ctime'], 'integer'],
            [['open_id'], 'string', 'max' => 160],
            [['union_id'], 'string', 'max' => 120],
            [['user_info'], 'default', 'value' => ''],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'id',
            'uid' => 'uid',
            'open_id' => 'open id',
            'union_id' => 'union id',
            'type' => 'type',
            'ctime' => 'ctime',
        ];
    }

    public static function getTypes() {
        return [self::TYPE_WEIBO, self::TYPE_QQ, self::TYPE_WEIXIN];
    }

    public function getUser() {
        return $this->hasOne(User::className(), ['id' => 'uid']);
    }

    public function savePlatform($data, $type) {
        $open_id = isset($data['openid']) ? $data['openid'] : $data['uid'];
        $union_id = isset($data['union_id']) ? $data['union_id'] : '';

        $model = static::findOne(['open_id' => $open_id, 'type' => $type]);
        if ($model) {
            if ($model->user) {
                $model->user->loginCookie();
                return static::retOK();
            }
        }

        $methods = [
            self::TYPE_QQ => 'qq',
            self::TYPE_WEIXIN => 'weixin',
            self::TYPE_WEIBO => 'weibo'
        ];
        $method = $methods[$type];

        $_user_data = $this->$method($data, $model);
        return $this->saveUserPlatform($_user_data, $type, $open_id, $union_id, $model);
    }

    public function qq($data) {
        $_data['uname'] = $data['nickname'];
        $_data['avatar'] = $data['figureurl_1'];
        return $_data;
    }

    public function weixin($data) {
        $_data['uname'] = $data['nickname'];
        $_data['avatar'] = $data['headimgurl'];
        return $_data;
    }

    public function weibo($data) {
        $_data['uname'] = $data['screen_name'];
        $_data['avatar'] = $data['avatar_large'];
        return $_data;
    }

    protected function saveUserPlatform($userData, $type, $open_id, $union_id = '', $model) {
        $user = new User();
        $user->setAttributes($userData);
        $res = $user->save(FALSE);
        if ($res['code'] != 0) {
            return $res;
        }

        if (!$model) {
            $model = new UserPlatform();
            $model->uid = $user->id;
            $model->type = $type;
            $model->open_id = $open_id;
            $model->union_id = $union_id;
            $res = $model->save();
            if ($res['code'] != 0) {
                return $res;
            }
        }

        $user->loginCookie();

        return $res;
    }
}
